<?php

namespace App\Http\Controllers\Project;

use App\Models\Project\PaymentTerm;
use App\Models\Project\Progress;
use App\Traits\Admin\SuccessOrFailure;
use Daijulong\LaravelRoles\Permission;
use Illuminate\Http\Request;


class ProgressController extends Controller
{
    use SuccessOrFailure;


    use SuccessOrFailure;

    private $permission = 'common.sign_company';


    public function index()
    {
        $this->canOrFailure($this->permission);
        $progresses = Progress::query()->with('approveRole')->orderBy('stage')->orderBy('sort')->orderBy('id')->get();
        return view('project.progress.index', compact('progresses'));
    }


    public function create()
    {
        $this->canOrFailure($this->permission);

        return view('project.progress.create');
    }

    public function store(Request $request)
    {
        $this->canOrFailure($this->permission);

        list($attributes, $approve_roles) = $this->buildData($request);
        $progress = Progress::create($attributes);
        $progress->approveRole()->sync($approve_roles);
        return $this->success('', route('admin.signCompany.index'));
    }

    public function edit($id)
    {
        $this->canOrFailure($this->permission);

        $progress = $this->getProgressOrFailure($id);

        $approve_roles = $progress->approveRole ? $progress->approveRole->pluck('id')->toArray() : [];

        return view('project.progress.edit', compact('progress', 'approve_roles'));
    }

    public function update(Request $request, $id)
    {
        $this->canOrFailure($this->permission);

        $progress = $this->getProgressOrFailure($id);
        list($attributes, $approve_roles) = $this->buildData($request);

        $progress->update($attributes);
        $progress->approveRole()->sync($approve_roles);

        return $this->success('', route('admin.signCompany.index'));
    }

    public function buildData($request)
    {
        $this->validate($request,
            ['name' => 'required|unique:pm_progresses,name,' . $request->input('id'), 'sort' => 'required|integer'],
            ['name.required' => '请填写进度名称！', 'name.unique' => '进度名称已被占用！', 'sort.required' => '请填写排序！', 'sort.integer' => '排序必须是数字！',]
        );
        $approve_roles = $request->input('approve_roles') ?? [];
        $attributes = [
            'stage' => $request->input('stage'),
            'name' => $request->input('name'),
            'need_approve' => $request->input('need_approve'),
            'approve_type' => $request->input('approve_type'),
            'need_add_charge' => $request->input('need_add_charge'),
//            'need_finance_approve' => $request->input('need_finance_approve'),
            'payment_term_id' => $request->input('payment_term_id') ?? 0,
            'sort' => $request->input('sort'),
            'approve_roles' => implode(',', $approve_roles),
        ];
        return [$attributes, $approve_roles];
    }

    public function destroy($id)
    {
        $this->canOrFailure($this->permission);

        $sign_company = $this->getProgressOrFailure($id);
        $sign_company->delete();
        return $this->success();
    }

    private function getProgressOrFailure($id)
    {
        $progress = Progress::query()->find($id);
        if (!$progress) {
            response($this->failure('值不存在！'), 404)->send();
            exit;
        }
        return $progress;
    }
}


